import type { App, Component } from 'vue';
import { defineAsyncComponent } from 'vue';
import { ElButton, ElCard, ElRow, ElCol, ElForm, ElFormItem, ElInput, ElSelect, ElSwitch, ElInputNumber, ElOption, ElDatePicker } from 'element-plus';

const PREFIX = './';
const PREFIX_NAMEONE = 'common/';
const PREFIX_NAMETWO = 'custom/';
const SUFFIX = '.vue';

export function globalInstall (app: App) {
  const importViews = import.meta.glob('./**/*.vue') as Record<string, ()=>  Promise<Component>>;

  Object.assign(importViews, { ElButton, ElCard, ElRow, ElCol, ElInput, ElFormItem, ElForm, ElInputNumber, ElSwitch, ElSelect, ElOption, ElDatePicker });

  for(const key in importViews){
    const comp = key.replace(PREFIX, '').replace(PREFIX_NAMEONE, '').replace(PREFIX_NAMETWO, '').replace(SUFFIX, '');

    if(typeof importViews[key] === 'function')  app.component(comp, defineAsyncComponent(importViews[key]));
    else  app.component(comp, importViews[key]);
  }
}
